package data_structure;

public class leetcode_0541 {
    public String reverseStr(String s, int k) {
        char[] list=s.toCharArray();
        int len=s.length();
        int num=len/(2*k);
        for(int i=0;i<num;++i){
            int left=i*2*k;
            int right=left+k-1;
            while(left<right){
                char a=list[left];
                list[left]=list[right];
                list[right]=a;
                left++;
                right--;
            }
        }
        int len2=len-num*2*k;
        if(len2/k==0){
            int left=num*2*k;
            int right=len-1;
            while(left<right){
                char a=list[left];
                list[left]=list[right];
                list[right]=a;
                left++;
                right--;
            }
        }
        else{
            int left=num*2*k;
            int right=left+k-1;
            while(left<right){
                char a=list[left];
                list[left]=list[right];
                list[right]=a;
                left++;
                right--;
            }
        }
        String result="";
        for(int i=0;i<len;++i){
            result+=list[i];
        }
        return result;
    }
}
